<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class AdminUser extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'password',
    ];
    protected $rememberTokenName = '';
    //用户有哪些角色
    public function roles()
    {
        // 1.目标主表   2.关联表  3.当前对象在关系表中外键 4.目标对象在关系表中外键
        return $this->belongsToMany(\App\AdminRole::class, 'admin_role_user', 'user_id', 'role_id')->withPivot(['user_id', 'role_id']);
    }
    //判断是否有某些角色
    public function isInRoles($roles)
    {
        return !!$roles->intersect($this->roles)->count();
    }
    //给用户分配角色
    public function assignRole($role)
    {
        return $this->roles()->save($role);
    }
    //取消用户分配角色
    public function deleteRole($role)
    {
        return $this->roles()->detach($role);
    }
    //是否有权限
    public function hasPermission($permission){
        return $this->isInRoles($permission->roles);
    }
}
